package com.zhang.linkNode;

/**
 * 双向链表、又称为双向环形链表
 * @param <T>
 */
public class DoubleLink<T> {

    class ZKDLink<T> {
        //记录前驱
        private ZKDLink head;
        //记录后驱
        private ZKDLink next;
        private T value;

        ZKDLink(T value) {
            this(value, null, null);
        }

        ZKDLink(T value, ZKDLink head, ZKDLink last) {
            this.value = value;
            this.head = head;
            this.next = last;
        }
    }

    private ZKDLink front;
    private ZKDLink rear;
    private ZKDLink head;
    private int size;

    DoubleLink() {

        front=new ZKDLink(null);
        rear=new ZKDLink(null);
        head = new ZKDLink(null);
        front.next=head;
        rear.next=head;
        size = 0;
    }

    public void show() {
        if (head != null) {
            ZKDLink zkdLink =head.next;
            System.out.println("查找：----------");
            while (zkdLink!=null) {
                System.out.print(zkdLink.value + " -> ");
                zkdLink = zkdLink.next;
            }
            System.out.println();
        }

    }
    public  int getSize()
    {
        return size;
    }


    public void add(T value) {
        if (value == null) {
            System.out.println("插入的内容为空");
            return;
        }
            ZKDLink newNode = new ZKDLink(value);
            rear.next.next=newNode;
            newNode.head=rear.next;
            rear.next=newNode;
            size++;
    }

}

class Tset1 {
    public static void main(String[] a) {
        DoubleLink<String> doubleLink = new DoubleLink();
        doubleLink.add("啊");
        doubleLink.add("我");
        System.out.println(doubleLink.getSize());
        doubleLink.show();
        System.out.println("-----------");

    }
}